package com.yan.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yan.controller.viewModel.login.UserModel;
import com.yan.controller.viewModel.looker.BookInfo;
import com.yan.controller.viewModel.looker.BookRatingInfo;
import com.yan.controller.viewModel.user.BorrowInfo;
import com.yan.controller.viewModel.user.ReserveInfo;
import com.yan.controller.viewModel.user.SuggestBuyInfo;
import com.yan.controller.viewModel.user.UserInfo;
import com.yan.util.CastMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yan.util.PageConstant;
import com.yan.dao.mapper.BooksMapper;
import com.yan.dao.entity.BookRating;
import com.yan.dao.entity.Books;
import com.yan.dao.entity.Borrows;
import com.yan.dao.entity.PersonMessage;
import com.yan.dao.entity.Reserve;
import com.yan.dao.entity.SuggestBuy;
import com.yan.dao.entity.Users;
import com.yan.service.BooksService;
import com.yan.vo.Page;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class BooksServiceImpl implements BooksService {
	@Autowired
	private BooksMapper booksMapper;

	/*
	 * 查询书籍表
	 */
	@Override
	public Page getAllBooks(BookInfo bookInfo) {
		Books book = (Books)CastMapper.CastTo(Books.class,bookInfo);
		//创建一个page对象
		Page page = new Page();
		assert book != null;
		if (book.getCurrentPage() == 0) {
			book.setCurrentPage(PageConstant.PAGE_NUM);//设置其当前页数为1
		}
		//设置检索开始行
		int start = (book.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		book.setStart(start);
		// List集合
		List<Books> list = booksMapper.getAllBooks(book);
		page.setList(list);
		
		int count = booksMapper.getCount(book);//得到书的数量
		int counPage = count / PageConstant.PAGE_SIZE;//得到总页数
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		//page.setCount(count);
		return page;
	}

	/*
	 * 通过图书的id查询图书
	 */
	@Override
	public BookInfo getBook(Integer id) {
		return (BookInfo) CastMapper.CastTo(BookInfo.class,booksMapper.getBook(id));
	}

	/*
	 * 插入一条借书数据到数据库中
	 */
	@Override
	public void insertBorrow(BorrowInfo borrow) {
		booksMapper.insertBorrow((Borrows) CastMapper.CastTo(Borrows.class,borrow));
	}

	/*
	 * 查询借书表
	 */
	@Override
	public Page getBorrows(BorrowInfo borrows) {
		// 开始
		Page page = new Page();
	    if (borrows.getCurrentPage() == 0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);//设置其当前页数为1
		}
				
		//设置检索开始行
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		
		// List集合
		List<Borrows> list = booksMapper.getBorrows((Borrows) CastMapper.CastTo(Borrows.class,borrows));


		page.setList(list);
		
		int count = booksMapper.getBorrowCount(borrows.getUid());//得到书的数量
		int counPage = count / PageConstant.PAGE_SIZE;//得到总页数
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		//page.setCount(count);		
		
		return page;
	}

	/*
	 * 查询还书表
	 */
	@Override
	public Page getBacks(BorrowInfo borrows) {
		// TODO Auto-generated method stub
		// 开始
		Page page = new Page();
	    if (borrows.getCurrentPage() == 0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);//设置其当前页数为1
		}
				
		//设置检索开始行
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		
		// List集合
		List<Borrows> list = booksMapper.getBacks((Borrows) CastMapper.CastTo(Borrows.class,borrows));
		page.setList(list);
		
		int count = booksMapper.getBorrowCount(borrows.getUid());//得到书的数量
		int counPage = count / PageConstant.PAGE_SIZE;//得到总页数
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);		
		
		return page;
	}

	/*
	 * 请求归还图书操作
	 */
	@Override
	public void updateBacks(Integer id) {
		// TODO Auto-generated method stub
		booksMapper.updateBacks(id);
	}

	/*
	 * 插入预定书籍到表中
	 */
	@Override
	public void insertReserve(ReserveInfo reserve) {
		// TODO Auto-generated method stub
		booksMapper.insertReserve((Reserve) CastMapper.CastTo(Reserve.class,reserve));
	}

	/*
	 * 查询预约表
	 */
	@Override
	public List<ReserveInfo> getReserveBook(Integer id) {
		// TODO Auto-generated method stub
		List<Reserve> reserveList = booksMapper.getReserveBook(id);
		List<ReserveInfo> reserveInfoList = new ArrayList<>(reserveList.size());
		for (Reserve reserve:reserveList){
			reserveInfoList.add((ReserveInfo) CastMapper.CastTo(ReserveInfo.class,reserve));
		}

		return reserveInfoList;
	}

	/*
	 * 查询用户是否存在
	 */
	@Override
	public UserModel selectUser(UserModel user) {
		// TODO Auto-generated method stub
		Users users = booksMapper.selectUser((Users)CastMapper.CastTo(Users.class,user));
		return (UserModel) CastMapper.CastTo(UserModel.class,users);
	}

	/*
	 * 完成提交建购功能
	 */
	@Override
	public void insertSuggest(SuggestBuyInfo suggest) {
		SuggestBuy suggestBuy = (SuggestBuy)CastMapper.CastTo(SuggestBuy.class,suggest);
		//查询此书是否已存在于表中
		SuggestBuy suggest01=booksMapper.selectSuggestBuy(suggestBuy);
		//如果此书有人申请过
		if(suggest01!=null) {
			booksMapper.updateSuggestBuy(suggest01.getId());//将申请数量加一
		} else {
			booksMapper.insertSuggestBuy(suggestBuy);//执行插入操作
		}
		
		
	}

	/*
	 * 完成个人信息的查询功能
	 */
	@Override
	public UserInfo selectPersonMessage(int uid) {
		//查询是否已存在此人的信息
		PersonMessage personMessage=booksMapper.selectPersonMessage(uid);
		UserInfo userInfo = (UserInfo) CastMapper.CastTo(UserInfo.class,personMessage);
		//如果没有
		if(personMessage==null) {
			//插入一条新数据
			booksMapper.insertPersonMessage(uid);
			return selectPersonMessage(uid);
		}else {
			return userInfo;
		}
		
	}

	/*
	 * 更新个人信息
	 */
	@Override
	public void updatePersonMessage(UserInfo personMessage) {
		
		booksMapper.updatePersonMessage((PersonMessage)CastMapper.CastTo(PersonMessage.class,personMessage));
	}
	
    /*
    * 插入指定用户的查询信息
    */
	@Override
	public void insertSelectInfo(BookInfo book) throws InstantiationException, IllegalAccessException {
		try {
			Books books = (Books) CastMapper.CastTo(Books.class,book);
			booksMapper.insertSelectInfo(books);
		}catch (Exception e){
			e.printStackTrace();
			throw e;
		}
	}

	/*
	 * 根据还书的id查询此图书的信息
	 */
	@Override
	public BookInfo selectBackedBook(Integer id) {
		Books book;
		book=booksMapper.selectBackedBook(id);
		return (BookInfo) CastMapper.CastTo(BookInfo.class,book);
	}

	@Override
	public void insertBookPingFen(BookRatingInfo bookrating) {
		booksMapper.insertBookPingFen((BookRating)CastMapper.CastTo(BookRating.class, bookrating));
		
	}

	@Override
	public void updateBackBookPingFen(Integer id) {
		booksMapper.updateBackBookToPingFen(id);
		
	}

	@Override
	public List<BookRatingInfo> selectBookRating(Integer id) {
		
		List<BookRating> list=booksMapper.selectBookRating(id);
		List<BookRatingInfo> bookRatingInfos = new ArrayList<>(list.size());
		for(BookRating bookRating:list){
			bookRatingInfos.add((BookRatingInfo) CastMapper.CastTo(BookRatingInfo.class,bookRating));
		}
		return bookRatingInfos;
	}

	@Override
	public List<BookRatingInfo> selectBookTuijian() {
		List<BookRating> list=booksMapper.selectBookTuijian();
		List<BookRatingInfo> bookRatingInfos = new ArrayList<>(list.size());
		for(BookRating bookRating:list){
			bookRatingInfos.add((BookRatingInfo) CastMapper.CastTo(BookRatingInfo.class,bookRating));
		}
		return bookRatingInfos;
	}

	@Override
	public List<BookRatingInfo> selectBookTuijian1(int userid) {
		List<BookRating> list=booksMapper.selectBookTuijian1(userid);
		List<BookRatingInfo> bookRatingInfos = new ArrayList<>(list.size());
		for(BookRating bookRating:list){
			bookRatingInfos.add((BookRatingInfo) CastMapper.CastTo(BookRatingInfo.class,bookRating));
		}
		return bookRatingInfos;
	}

	@Override
	public String clickUp(int uid, int bkid,int suid) {
		String end;
		Integer type=booksMapper.selectBookratingType(uid,bkid,suid);
		
		if(type==null){
			booksMapper.updateUpType(uid, bkid, suid, 1);
			booksMapper.updateUpAdd(uid, bkid);
			end=""+booksMapper.selectUpNum(uid, bkid);
			booksMapper.insertUoDnote(uid, bkid, suid, 1);
		}else if(type==1) {
			booksMapper.updateUpType(uid, bkid, suid, 3);
			booksMapper.updateUpDec(uid, bkid);
			end=""+booksMapper.selectUpNum(uid, bkid);
		}else if(type==0){
			booksMapper.updateUpType(uid, bkid, suid, 1);
			booksMapper.updateDownDec(uid, bkid);
			booksMapper.updateUpAdd(uid, bkid);
			end=""+booksMapper.selectUpNum(uid, bkid);
		}else {
			booksMapper.updateUpType(uid, bkid, suid, 1);
			booksMapper.updateUpAdd(uid, bkid);
			end=""+booksMapper.selectUpNum(uid, bkid);
		}
		
		return end;
	}

	@Override
	public String clickDown(int uid, int bkid, int suid) {
		String end;
		Integer type=booksMapper.selectBookratingType(uid,bkid,suid);
		if(type==null){
			booksMapper.updateUpType(uid, bkid, suid, 0);
			booksMapper.updateDownAdd(uid, bkid);
			end=""+booksMapper.selectDownNum(uid, bkid);
			booksMapper.insertUoDnote(uid, bkid, suid, 0);
		}else if(type==1) {
			booksMapper.updateUpType(uid, bkid, suid, 0);
			booksMapper.updateUpDec(uid, bkid);
			booksMapper.updateDownAdd(uid, bkid);
			end=""+booksMapper.selectDownNum(uid, bkid);
		}else if(type==0){
			booksMapper.updateUpType(uid, bkid, suid, 3);
			booksMapper.updateDownDec(uid, bkid);
			end=""+booksMapper.selectDownNum(uid, bkid);
		}else {
			booksMapper.updateUpType(uid, bkid, suid, 0);
			booksMapper.updateDownAdd(uid, bkid);
			end=""+booksMapper.selectDownNum(uid, bkid);
		}
		
		return end;
	}
	
	
}
